﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Symbonix.NorSoNet.Framework.DAL.Entities;
using Symbonix.NorSoNet.Framework.DAL.Repositories;
using System.Data.SqlTypes;
using System.Collections;

namespace Symbonix.NorSoNet.Framework.BLL
{
    public class EmailTemplate : EmailTemplateBase
    {
        #region Properties

        public override int Id
        {
            get;
            protected set;
        }

        public override string Name
        {
            get;
            set;
        }

        public override string Subject
        {
            get;
            set;
        }

        public override string Body
        {
            get;
            set;
        }

        public override DateTime? LastUpdate
        {
            get;
            set;
        }

        #endregion

        protected EmailTemplate() { }

        protected internal static IEmailTemplate GetInstance() { return new EmailTemplate(); }

        public override IEmailTemplate Load(int id)
        {

            EmailTemplateDataRepository etdr = new EmailTemplateDataRepository();
            EmailTemplateData etd = etdr.GetByPrimaryKey(id);
            return GetBizEmailTemplate(etd);
        }

        public override IEmailTemplate Update()
        {
            EmailTemplateData etd = CreateOrUpdateEmailTemplateEntity(Id, Name, Subject, Body, true);
            this.LastUpdate = etd.LastUpdate;
            return this;
        }

        public override IEmailTemplate Create(string name, string subject, string body)
        {
            EmailTemplateDataRepository etdr = new EmailTemplateDataRepository();
            if (!etdr.IsNameExits(name))
            {
                EmailTemplateData etd = CreateOrUpdateEmailTemplateEntity(0,name, subject, body, false);
                return GetBizEmailTemplate(etd);
            }
            else
            {
                throw new InvalidOperationException("duplicate name");
            }

        }

        public override bool Delete(int id)
        {
            EmailTemplateDataRepository etdr = new EmailTemplateDataRepository();
            return etdr.Delete(id);
        }

        public override bool Delete()
        {
            EmailTemplateDataRepository etdr = new EmailTemplateDataRepository();
            return etdr.Delete(this.Id);
        }

        public override bool Delete(string name)
        {
            EmailTemplateDataRepository etdr = new EmailTemplateDataRepository();
            EmailTemplateData etd = etdr.GetByName(name);
            return etdr.DeleteEmailTemplateByName(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="etData"></param>
        /// <returns></returns>
        private IEmailTemplate GetBizEmailTemplate(EmailTemplateData etData)
        {
            EmailTemplate et = new EmailTemplate();
            et.Body = etData.Body;
            et.Name = etData.Name;
            et.Subject = etData.Subject;
            et.LastUpdate = etData.LastUpdate;
            et.Id = etData.EmailTemplateID;
            return et;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isUpdate"></param>
        /// <returns></returns>
        private EmailTemplateData CreateOrUpdateEmailTemplateEntity(int id, string name, string subject, string body, bool isUpdate)
        {
            EmailTemplateDataRepository etdr = new EmailTemplateDataRepository();
            if (isUpdate)
            {
                EmailTemplateData etd = etdr.GetByPrimaryKey(id);
                etd.Body = body;
                etd.Name = name;
                etd.Subject = subject;
                etd.LastUpdate = DateTime.Now;
                return etdr.Update(etd);
            }
            else
            {
                EmailTemplateData etd = new EmailTemplateData();
                etd.EmailTemplateID = id;
                etd.Body = body;
                etd.Name = name;
                etd.Subject = subject;
                etd.LastUpdate = DateTime.Now;
                return etdr.Insert(etd);
            }
        }

        public override List<IEmailTemplate> GetAllTemplates()
        {
            EmailTemplateDataRepository emailTemplateDataRepository = new EmailTemplateDataRepository();
            int total = 0;
            IList emailTemplateData = emailTemplateDataRepository.GetAll("", false, false, 0, 0, out total);
            List<IEmailTemplate> emailTemplates = new List<IEmailTemplate>();
            if (emailTemplateData != null && emailTemplateData.Count > 0)
            {
                foreach (EmailTemplateData template in emailTemplateData)
                {
                    emailTemplates.Add(GetBizEmailTemplate(template));
                }
            }
            return emailTemplates;
        }
    }
}
